//package com.plian.system.service.bpa.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.plian.Tools.CollectionUtil;
//import com.plian.Tools.StringUtil;
//import com.plian.Tools.UuidUtil;
//import com.plian.system.config.exception.MyRuntimeException;
//import com.plian.system.dto.bpa.BpaFinancialPerformanceDTO;
//import com.plian.system.entity.bpa.BpaFinancialPerformance;
//import com.plian.system.entity.bpa.BpaFinancialPerformanceGear;
//import com.plian.system.entity.bpa.BpaIndustryCreditConfig;
//import com.plian.system.entity.pf.AppraisalIndex;
//import com.plian.system.entity.sys.Dict;
//import com.plian.system.mapper.bpa.BpaFinancialPerformanceGearMapper;
//import com.plian.system.mapper.bpa.BpaFinancialPerformanceMapper;
//import com.plian.system.mapper.bpa.BpaIndustryCreditConfigMapper;
//import com.plian.system.mapper.pf.AppraisalIndexMapper;
//import com.plian.system.mapper.sys.DictMapper;
//import com.plian.system.model.CommonCode;
//import com.plian.system.model.ListResult;
//import com.plian.system.model.ObjResult;
//import com.plian.system.model.R;
//import com.plian.system.mybatis.search.SearchEntity;
//import com.plian.system.mybatis.support.Condition;
//import com.plian.system.mybatis.support.Query;
//import com.plian.system.service.bpa.IBpaFinaPerformanceIndiConfigService;
//import com.plian.system.vo.bpa.BpaFinancialPerformanceGearVO;
//import com.plian.system.vo.bpa.BpaFinancialPerformanceVO;
//import com.plian.system.vo.bpa.BpaIndustryCreditConfigVO;
//import com.plian.system.wrapper.bpa.BpaFinancialPerformanceGearWrapper;
//import com.plian.system.wrapper.bpa.BpaFinancialPerformanceWrapper;
//import com.plian.system.wrapper.bpa.BpaIndustryCreditConfigWrapper;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import javax.annotation.Resource;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//import java.util.stream.Collectors;
//
///**
// * @author jiatp
// * @create 2022-12-16
// **/
//@Slf4j
//@Service
//public class BpaFinaPerformanceIndiConfigServiceImpl extends ServiceImpl<BpaFinancialPerformanceMapper, BpaFinancialPerformance> implements IBpaFinaPerformanceIndiConfigService {
//
//
//    @Resource
//    private BpaFinancialPerformanceWrapper financialPerformanceWrapper;
//    @Resource
//    private BpaFinancialPerformanceGearWrapper financialPerformanceGearWrapper;
//    @Resource
//    private BpaIndustryCreditConfigWrapper industryCreditConfigWrapper;
//
//    @Resource
//    private AppraisalIndexMapper appraisalIndexMapper;
//    @Resource
//    private BpaFinancialPerformanceGearMapper performanceGearMapper;
//    @Resource
//    private BpaIndustryCreditConfigMapper industryCreditConfigMapper;
//    @Resource
//    private DictMapper dictMapper;
//
//    @Transactional(rollbackFor = Throwable.class)
//    @Override
//    public R submit(BpaFinancialPerformanceVO finaPerformanceIndiConfigVO) {
//
//        BpaFinancialPerformance financialPerformance = financialPerformanceWrapper.voToEntity(finaPerformanceIndiConfigVO);
//        List<BpaFinancialPerformanceGearVO> dwVOList = finaPerformanceIndiConfigVO.getDwList();
//        List<BpaFinancialPerformanceGear> bpaFinancialPerformanceGearList = financialPerformanceGearWrapper.voToEntity(dwVOList);
//        List<BpaIndustryCreditConfig> bpaIndustryCreditConfigList = industryCreditConfigWrapper.voToEntity(finaPerformanceIndiConfigVO.getCreditList());
//        try {
//            Dict dictIndicatCode = dictMapper.selectOne(new LambdaQueryWrapper<Dict>().eq(Dict::getCode, finaPerformanceIndiConfigVO.getDictIndicatorsCode()));
//            financialPerformance.setDictIndicatorsCode(dictIndicatCode.getCode());
//            financialPerformance.setDictIndicatorsName(dictIndicatCode.getName());
//            if (StringUtil.isNotBlank(financialPerformance.getIndicatorsId())){
//                AppraisalIndex indicatorsCodes = appraisalIndexMapper.selectOne(new LambdaQueryWrapper<AppraisalIndex>().eq(AppraisalIndex::getId, finaPerformanceIndiConfigVO.getIndicatorsId()));
//                financialPerformance.setIndicatorsId(String.valueOf(indicatorsCodes.getId()));
//                financialPerformance.setIndicatorsName(indicatorsCodes.getName());
//            }else{
//                financialPerformance.setIndicatorsName(finaPerformanceIndiConfigVO.getIndicatorsName());
//            }
//            String mainId = null;
//            //主表
//            if (financialPerformance == null || StringUtil.isBlank(financialPerformance.getId())) {
//                mainId = UuidUtil.get32UUID();
//                financialPerformance.setId(mainId);
//                generateMain(finaPerformanceIndiConfigVO, financialPerformance);
//                financialPerformance.setCreateTime(new Date());
//                financialPerformance.setUpdateTime(new Date());
//                financialPerformance.setStatus(0);
//                baseMapper.insert(financialPerformance);
//                log.info("update 档位配置表...");
//                /**
//                 * 档位配置表
//                 **/
//                for (BpaFinancialPerformanceGear item : bpaFinancialPerformanceGearList) {
//                    genGear(item);
//                    if (StringUtil.isEmpty(item.getId())) {
//                        item.setId(UuidUtil.get32UUID());
//                        item.setFpid(mainId);
//                        item.setCreateTime(new Date());
//                        performanceGearMapper.insert(item);
//                    } else {
//                        item.setUpdateTime(new Date());
//                        performanceGearMapper.updateById(item);
//                    }
//                }
//            } else {
//                log.info("修改功能操作...");
//                mainId = financialPerformance.getId();
//                BpaFinancialPerformance currPerformance = baseMapper.selectOne(new LambdaQueryWrapper<BpaFinancialPerformance>()
//                        .eq(BpaFinancialPerformance::getId, mainId));
//                if (StringUtil.isNotBlank(finaPerformanceIndiConfigVO.getMainScoreStandard())) {
//                    currPerformance.setScoreStandard(finaPerformanceIndiConfigVO.getMainScoreStandard());
//                }
//                generateMain(finaPerformanceIndiConfigVO, currPerformance);
//                currPerformance.setUpdateTime(new Date());
//                currPerformance.setScoreStandard(finaPerformanceIndiConfigVO.getScoreStandard());
//                baseMapper.updateById(currPerformance);
//
//                if (CollectionUtil.isNotEmpty(bpaFinancialPerformanceGearList)) {
//                    bpaFinancialPerformanceGearList.forEach(item -> {
//                        genGear(item);
//                        if (StringUtil.isEmpty(item.getId())) {
//                            item.setId(UuidUtil.get32UUID());
//                            item.setFpid(currPerformance.getId());
//                            item.setUpdateTime(new Date());
//                            item.setCreateTime(new Date());
//                            performanceGearMapper.insert(item);
//                        } else {
//                            item.setUpdateTime(new Date());
//                            performanceGearMapper.updateById(item);
//                        }
//                    });
//                }
//                if (CollectionUtil.isNotEmpty(bpaIndustryCreditConfigList)) {
//                    bpaIndustryCreditConfigList.forEach(item -> {
//                        genCredit(item);
//                        if (StringUtil.isEmpty(item.getId())) {
//                            item.setId(UuidUtil.get32UUID());
//                            item.setFpid(currPerformance.getId());
//                            item.setUpdateTime(new Date());
//                            item.setCreateTime(new Date());
//                            industryCreditConfigMapper.insert(item);
//                        } else {
//                            item.setUpdateTime(new Date());
//                            industryCreditConfigMapper.updateById(item);
//                        }
//                    });
//                }
//            }
//            return R.success();
//        } catch (Exception e) {
//            log.error("error:保存或修改失败 {} : ", e);
//            throw new MyRuntimeException(new ObjResult(CommonCode.FAIL));
//        }
//    }
//
//    /**
//     * 生成计分子表code
//     *
//     * @param item
//     */
//    private void genCredit(BpaIndustryCreditConfig item) {
//        if (StringUtil.isNotBlank(item.getCompareTypeCode())) {
//            Dict compareTypeDict = dictMapper.selectOne(new LambdaQueryWrapper<Dict>().eq(Dict::getCode, item.getCompareTypeCode()));
//            item.setCompareTypeName(compareTypeDict.getName());
//        }
//        if (StringUtil.isNotBlank(item.getCompareTypeCode())) {
//            Dict compareStandardDict = dictMapper.selectOne(new LambdaQueryWrapper<Dict>().eq(Dict::getCode, item.getCompareStandardCode()));
//            item.setCompareStandardName(compareStandardDict.getName());
//        }
//    }
//
//    /**
//     * 生成档位子表code
//     *
//     * @param item
//     */
//    private void genGear(BpaFinancialPerformanceGear item) {
//        if (StringUtil.isNotBlank(item.getIndustryCode())) {
//            Dict industryDict = dictMapper.selectOne(new LambdaQueryWrapper<Dict>().eq(Dict::getCode, item.getIndustryCode()));
//            item.setIndustryName(industryDict.getName());
//            item.setIndustryCode(industryDict.getCode());
//        }
//        if (StringUtil.isNotBlank(item.getUnitCode())) {
//            Dict unitDict = dictMapper.selectOne(new LambdaQueryWrapper<Dict>().eq(Dict::getCode, item.getUnitCode()));
//            item.setUnitName(unitDict.getName());
//            item.setUnitCode(unitDict.getCode());
//        }
//    }
//
//
//    /**
//     * 赋值code
//     *
//     * @param finaPerformanceIndiConfigVO
//     * @param financialPerformance
//     */
//    private void generateMain(BpaFinancialPerformanceVO finaPerformanceIndiConfigVO, BpaFinancialPerformance financialPerformance) {
//        //指标id不为空 下拉情况
//        if (StringUtil.isEmpty(finaPerformanceIndiConfigVO.getIndicatorsId())) {
//            financialPerformance.setIndicatorsId("");
//            financialPerformance.setIndicatorsName(finaPerformanceIndiConfigVO.getIndicatorsName());
//        } else {
//            AppraisalIndex appraisalIndex = appraisalIndexMapper.selectOne(new LambdaQueryWrapper<AppraisalIndex>()
//                    .eq(AppraisalIndex::getId, finaPerformanceIndiConfigVO.getIndicatorsId()));
//            financialPerformance.setIndicatorsId(String.valueOf(appraisalIndex.getId()));
//            financialPerformance.setIndicatorsName(appraisalIndex.getName());
//        }
//    }
//
//    @Override
//    public ListResult pageList(Query query) {
//        ListResult<BpaFinancialPerformanceDTO> listResult;
//        try {
//            List<SearchEntity> search = query.getSearch();
//            BpaFinancialPerformanceVO financialPerformanceVO = new BpaFinancialPerformanceVO();
//            if (CollectionUtil.isNotEmpty(search)) {
//                for (SearchEntity entity : search) {
//                    if ("indicatorsId".equals(entity.getField())) {
//                        financialPerformanceVO.setIndicatorsId(entity.getValue().toString());
//                    }
//                    if ("dictIndicatorsCode".equals(entity.getField())) {
//                        financialPerformanceVO.setDictIndicatorsCode(entity.getValue().toString());
//                    }
//                    if ("isYear".equals(entity.getField())) {
//                        financialPerformanceVO.setIsYear(entity.getValue().toString());
//                    }
//                }
//            }
//            IPage<BpaFinancialPerformanceDTO> pages = baseMapper.pageList(Condition.getPage(query), financialPerformanceVO);
//            listResult = new ListResult<>(CommonCode.SUCCESS);
//            listResult.setList(pages.getRecords());
//            listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
//        } catch (Exception e) {
//            log.error("error:查询失败：" + e.getMessage());
//            throw new MyRuntimeException(new ObjResult(CommonCode.ERROR));
//        }
//        return listResult;
//    }
//
//    @Transactional(rollbackFor = Throwable.class)
//    @Override
//    public R remove(BpaFinancialPerformanceVO finaPerformanceIndiConfigVO) {
//        try {
//            //1 主表id为空，档位id、计分id不为空
//            if (CollectionUtil.isEmpty(finaPerformanceIndiConfigVO.getIds())) {
//                if (CollectionUtil.isNotEmpty(finaPerformanceIndiConfigVO.getDwIds())) {
//                    performanceGearMapper.deleteBatchIds(finaPerformanceIndiConfigVO.getDwIds());
//                    log.info("删除档位配置成功！");
//                }
//                if (CollectionUtil.isNotEmpty(finaPerformanceIndiConfigVO.getCreditIds())) {
//                    industryCreditConfigMapper.deleteBatchIds(finaPerformanceIndiConfigVO.getCreditIds());
//                    log.info("删除计分配置成功！");
//                }
//            }
//            //2 主表id不为空，档位id、计分id为空
//            if (CollectionUtil.isNotEmpty(finaPerformanceIndiConfigVO.getIds())) {
//                List<BpaFinancialPerformance> bpaFinancialPerformances = baseMapper.selectBatchIds(finaPerformanceIndiConfigVO.getIds());
//
//                List<String> ids = bpaFinancialPerformances.stream().map(BpaFinancialPerformance::getId).collect(Collectors.toList());
//                if (CollectionUtil.isNotEmpty(bpaFinancialPerformances)) {
//
//                    List<BpaFinancialPerformanceGear> bpaFinancialPerformanceGearList = performanceGearMapper.selectList(new LambdaQueryWrapper<BpaFinancialPerformanceGear>().in(BpaFinancialPerformanceGear::getFpid, ids));
//                    if (CollectionUtil.isNotEmpty(bpaFinancialPerformanceGearList)) {
//                        List<String> gearIds = bpaFinancialPerformanceGearList.stream().map(BpaFinancialPerformanceGear::getId).collect(Collectors.toList());
//                        performanceGearMapper.deleteBatchIds(gearIds);
//                        log.info("删除档位配置成功！");
//                    }
//                    List<BpaIndustryCreditConfig> bpaIndustryCreditConfigs = industryCreditConfigMapper.selectList(new LambdaQueryWrapper<BpaIndustryCreditConfig>().in(BpaIndustryCreditConfig::getFpid, ids));
//                    if (CollectionUtil.isNotEmpty(bpaIndustryCreditConfigs)) {
//                        List<String> creditIds = bpaIndustryCreditConfigs.stream().map(BpaIndustryCreditConfig::getId).collect(Collectors.toList());
//                        industryCreditConfigMapper.deleteBatchIds(creditIds);
//                        log.info("删除计分配置成功！");
//                    }
//                    baseMapper.deleteBatchIds(finaPerformanceIndiConfigVO.getIds());
//                }
//
//            }
//
//        } catch (Exception e) {
//            log.error("error:删除失败..." + e.getMessage());
//            throw new MyRuntimeException(new ObjResult(CommonCode.FAIL));
//        }
//        return R.success();
//    }
//
//    @Override
//    public R detail(BpaFinancialPerformanceVO finaPerformanceIndiConfigVO) {
//        BpaFinancialPerformance bpaFinancialPerformance = baseMapper.selectById(finaPerformanceIndiConfigVO.getId());
//        if (bpaFinancialPerformance == null) {
//            return R.data("未查询到当前配置！", CommonCode.FAIL);
//        }
//
//        BpaFinancialPerformanceVO financialPerformanceVO = financialPerformanceWrapper.entityToVO(bpaFinancialPerformance);
//        List<BpaFinancialPerformanceGearVO> dwVOList = new ArrayList<>();
//        List<BpaIndustryCreditConfigVO> creditVOList = new ArrayList<>();
//
//        if (bpaFinancialPerformance != null) {
//            List<BpaFinancialPerformanceGear> gearList = performanceGearMapper.selectList(new LambdaQueryWrapper<BpaFinancialPerformanceGear>().in(BpaFinancialPerformanceGear::getFpid, bpaFinancialPerformance.getId()));
//            if (CollectionUtil.isNotEmpty(gearList)) {
//                dwVOList = financialPerformanceGearWrapper.entityToVO(gearList);
//            }
//
//            List<BpaIndustryCreditConfig> creditList = industryCreditConfigMapper.selectList(new LambdaQueryWrapper<BpaIndustryCreditConfig>().in(BpaIndustryCreditConfig::getFpid, bpaFinancialPerformance.getId()));
//            if (CollectionUtil.isNotEmpty(creditList)) {
//                creditVOList = industryCreditConfigWrapper.entityToVO(creditList);
//            }
//        }
//        financialPerformanceVO.setDwList(dwVOList);
//        financialPerformanceVO.setCreditList(creditVOList);
//
//        return R.data(financialPerformanceVO, CommonCode.SUCCESS);
//    }
//
//    @Override
//    public R setScoreConfig(BpaIndustryCreditConfigVO industryCreditConfigVO) {
//        //指标主表id
//        String mainId = industryCreditConfigVO.getMainId();
//        if (StringUtil.isBlank(mainId)) {
//            return R.data("id不能为空", CommonCode.FAIL);
//        }
//        BpaFinancialPerformance bpaFinancialPerformance = baseMapper.selectById(mainId);
//        //保存主表id
//        bpaFinancialPerformance.setScoreStandard(industryCreditConfigVO.getMainScoreStandard());
//        baseMapper.updateById(bpaFinancialPerformance);
//        log.info("保存计分标准成功...");
//        List<BpaIndustryCreditConfig> scoreList = industryCreditConfigVO.getScoreList();
//        if (CollectionUtil.isEmpty(scoreList)) {
//            return R.data("计分配置不能为空", CommonCode.FAIL);
//        }
//        scoreList.forEach(item -> {
//            item.setId(UuidUtil.get32UUID());
//            item.setFpid(mainId);
//            Dict compareTypeDict = dictMapper.selectOne(new LambdaQueryWrapper<Dict>().eq(Dict::getCode, item.getCompareTypeCode()));
//            Dict compareStandardDict = dictMapper.selectOne(new LambdaQueryWrapper<Dict>().eq(Dict::getCode, item.getCompareStandardCode()));
//            item.setCompareStandardName(compareStandardDict.getName());
//            item.setCompareTypeName(compareTypeDict.getName());
//            item.setCreateTime(new Date());
//            item.setUpdateTime(new Date());
//            industryCreditConfigMapper.insert(item);
//        });
//        return R.success();
//    }
//
//    @Override
//    public R invalid(BpaFinancialPerformanceVO finaPerformanceIndiConfigVO) {
//        if (CollectionUtil.isEmpty(finaPerformanceIndiConfigVO.getIds())) {
//            return R.data("id不能为空", CommonCode.FAIL);
//        }
//        List<BpaFinancialPerformance> bpaFinancialPerformances = baseMapper.selectBatchIds(finaPerformanceIndiConfigVO.getIds());
//        bpaFinancialPerformances.forEach(item -> {
//            item.setStatus(2);
//            baseMapper.updateById(item);
//        });
//        return R.success();
//    }
//
//    @Override
//    public R application(BpaFinancialPerformanceVO finaPerformanceIndiConfigVO) {
//        if (CollectionUtil.isEmpty(finaPerformanceIndiConfigVO.getIds())) {
//            return R.data("id不能为空", CommonCode.FAIL);
//        }
//        List<BpaFinancialPerformance> bpaFinancialPerformances = baseMapper.selectBatchIds(finaPerformanceIndiConfigVO.getIds());
//        bpaFinancialPerformances.forEach(item -> {
//            item.setStatus(1);
//            baseMapper.updateById(item);
//        });
//        return R.success();
//    }
//}
